<?php
/**
 * Create by 11li
 * Date 2020/4/27
 * Time 21:42
 *
 */

namespace app\common\service;


use app\common\model\ActivityReservation;
use app\common\model\Order;
use app\common\model\Reservation;
use think\Db;
use think\Exception;

class ReservationService
{
	/**
	 * 获取商品活动ID
	 * @param int $goodsId
	 * @param $userId
	 * @return array|false|\PDOStatement|string|\think\Model
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public static function getActivityByGoodsId( $goodsId, $userId)
	{
		$row = Db::name('activityReservation')
			->where(['goods_id' => $goodsId, 'status' => StatusService::STATUS_NORMAL])
			->find();
		
		$row['order_status'] = self::getOrderStatus($row['id'], $userId);
		return $row;
		
	}
	
	/**
	 * 获取预约活动的记录ID
	 * @param int $activityId
	 * @return mixed|string
	 * @throws Exception
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	public static function getReservationIdByActivityId(int $activityId) {
		$row = Db::name('reservation')
			->where([
				'activity_id' => $activityId,
				'status' => StatusService::RESERVATION_ING
			])
			->order('id', 'desc')
			->find();
		if(!$row) {
			$activity = ActivityReservation::get($activityId);
			$model = new Reservation();
			$res = $model->isUpdate(false)
				->save([
					'activity_id' => $activityId,
					'goods_id' => $activity['goods_id'],
					'number' => $activity['number'],
					'expire_time' => time()+($activity['limit_hours']*3600)
				]);
			if(!$res) {
				throw new Exception('添加预约活动出错');
			}
			$id = $model->getLastInsID();
		} else {
			$id = $row['id'];
		}
		
		return $id;
	}
	
	/**
	 * 获取用户拼团状态
	 * @param $activityId
	 * @param int $userId
	 * @return int|mixed
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	private static function getOrderStatus($activityId, $userId)
	{
		$status = -1;
		$order =  Db::name('order')
			->where([
				'user_id' => $userId,
				'order_type' => StatusService::ORDER_TYPE_RESERVATION,
				'activity_id' => $activityId,
				'status' => ['in', [StatusService::ORDER_PAYMENT_STATUS_WAIT, StatusService::ORDER_RESERVATION_ING, StatusService::ORDER_WAIT_LAST]]])
			->order('id', 'desc')
			->find();
		if($order) {
			$status = $order['status'];
		}
		
		return $status;
	}
	
	
	/**
	 * 添加预约数量
	 * @param $record_id
	 * @throws Exception
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 * @throws \think\exception\PDOException
	 */
	public static function addNumber($record_id)
	{
		$row = Db::name('reservation')->find($record_id);
		$number = $row['buy_number'] + 1;
		$data = [
			'buy_number' => $number,
			'update_time' => time()
		];
		if($number >= $row['number']) {
			$data['status'] = StatusService::RESERVATION_WAIT_PAY;
		}
		//添加预约人数
		$reservationRes = Db::name('reservation')
			->where(['id' => $record_id])
			->update($data);
		if(!$reservationRes) {
			throw new Exception('添加预约人数出错');
		}
		
		//预约成功,更改订单状态
		if(isset($data['status'])) {
			$activity = ActivityReservation::get($row['activity_id']);
			$orderRes = Db::name('order')
				->where(['record_id' => $record_id])
				->update([
					'status' => StatusService::ORDER_WAIT_LAST,
					'expire_time' => time()+($activity['pay_hours']*3600),
					'update_time' => time()
				]);
			if(!$orderRes) {
				throw new Exception('更改订单状态出错');
			}
			//添加队列发通知
		}
	}
	
	/**
	 * 添加支付人数
	 * @param $record_id
	 * @throws Exception
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 * @throws \think\exception\PDOException
	 */
	public static function addPayNumber($record_id)
	{
		$row = Db::name('reservation')->find($record_id);
		$number = $row['pay_number'] + 1;
		$data = [
			'pay_number' => $number,
			'update_time' => time()
		];
		if($number >= $row['pay_number']) {
			$data['status'] = StatusService::RESERVATION_SUCCESS;
			$data['success_time'] = time();
		}
		//添加预约人数
		$reservationRes = Db::name('reservation')
			->where(['id' => $record_id])
			->update($data);
		if(!$reservationRes) {
			throw new Exception('添加支付人数出错');
		}
		
		//预约成功,更改订单状态
		if(isset($data['status'])) {
			$orderRes = Db::name('order')
				->where(['record_id' => $record_id, 'status' => StatusService::ORDER_WAIT_LAST_OTHER])
				->update([
					'status' => StatusService::ORDER_WAIT_SHIP,
					'update_time' => time()
				]);
			if(!$orderRes) {
				throw new Exception('更改订单状态出错');
			}
			//添加队列发通知
		}
	}
}